import sys

import pygame.mixer
import setup
from mario import *
from tools.water_pipe import *


def main():
    bg_size = bg_width, bg_height = 840, 448
    bg_x = 0  # 屏幕移动的长度
    floor_height = 44
    pygame.init()  # 初始化pygame
    screen = pygame.display.set_mode(bg_size)
    pygame.display.set_caption("测试版本")  # 设置标题
    screen.fill((0, 0, 0))
    img_background = pygame.image.load(r"Img/level_1.png")
    img_background = pygame.transform.rotozoom(img_background, 0, 2)
    screen.blit(img_background, (0, 0))
    # 导入音乐
    pygame.mixer.music.load(r"music/main_theme.ogg")
    pygame.mixer.music.play()
    clock = pygame.time.Clock()
    mario = Mario((0, bg_height - floor_height))

    while True:
        clock.tick(30)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
        # 背景移动
        if mario.rect.left > bg_width / 2:
            bg_x -= mario.speed
            mario.rect.left -= mario.speed
        # 越界判定
        if mario.rect.left < 0:
            mario.rect.left = 0

        # 实例化管道
        setup.setup_pipe(bg_width, -bg_x, mario)

        # 实例化洞
        setup.setup_hole(bg_width, -bg_x, mario)

        # 马里奥控制
        mario.mario_jump(bg_height, screen)
        screen.blit(img_background, (bg_x, 0))
        screen.blit(mario.image, mario.rect)
        setup.setup_goomba(screen, mario, bg_width, -bg_x)
        pygame.display.update()


# 按间距中的绿色按钮以运行脚本。
if __name__ == '__main__':
    main()
